/*
 * @Descripttion:
 * @version: 1.0.0
 * @Author: htang
 * @Date: 2023-10-10 10:47:09
 * @LastEditors: htang
 * @LastEditTime: 2024-01-19 17:47:14
 */
import { defineStore } from 'pinia';
import { store } from '@/store';
import { getTopology, getOriginalData, setOriginalData, getVariableData, setVariableData } from '@/utils/meta-storage'
import { getGraphicGroups, setGraphicGroups } from '@/utils/meta-storage'

export const useCommonStore = defineStore('common', {
  state: () => ({
    topology: getTopology() || {},
    graphics: getGraphicGroups() || {},
    // 原始数据
    originalData: getOriginalData() || {},
    variableData: getVariableData() || {},
  }),
  getters: {
    getTopology(state) {
      return state.topology || {};
    },
    getGraphicGroups(state) {
      return state.graphics || {};
    },
    getOriginalData(state) {
      return state.originalData || {};
    },
    getVariableData(state) {
      return state.variableData || {};
    }
  },
  actions: {
    setTopology(data) {
      this.topology = data;
    },
    setGraphicGroups(data) {
      this.graphics = data;
      setGraphicGroups(JSON.stringify(data));
    },
    setOriginalData(data) {
      this.originalData = data;
      setOriginalData(data);
    },
    setVariableData(data) {
      this.variableData = data;
      setVariableData(data);
    }
  }
})

// Need to be used outside the setup
export function useCommonStoreWithOut() {
  return useCommonStore(store);
}